package fr.mywiki.model.ejbAdapter.services;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.ObjectNotFoundException;
import javax.ejb.RemoveException;
import javax.ejb.SessionBean;

import fr.mywiki.model.ejbAdapter.EntityHomeCache;
import fr.mywiki.model.ejbAdapter.ServicesAdapter;
import fr.mywiki.model.ejbAdapter.entity.AccountLocal;
import fr.mywiki.model.ejbAdapter.entity.EntryLocal;
import fr.mywiki.model.ejbAdapter.entity.StructureLocal;
import fr.mywiki.model.ejbAdapter.entity.VersionLocal;
import fr.mywiki.model.ejbAdapter.services.business.RemoveServiceBusiness;

/**
 * This session bean deals with adding links to bean objects.
 * 
 * @ejb.bean name = "RemoveService" 
 * 		display-name = "RemoveService"
 * 		description = "RemoveService Administration Service"
 * 		jndi-name = "ejb/MyWiki/RemoveService"
 * 		remote-business-interface = "fr.mywiki.model.ejbAdapter.services.business.RemoveServiceBusiness"
 * 
 * @ejb.home
 * 		extends = "javax.ejb.EJBHome"
 * 		package = "fr.mywiki.model.ejbAdapter.services"
 *  
 * @ejb.interface
 * 		extends = "javax.ejb.EJBObject"
 * 		package = "fr.mywiki.model.ejbAdapter.services"
 * 
 * @author tlombard
 */
public class RemoveServiceBean extends ServicesAdapter implements RemoveServiceBusiness,SessionBean {
	/**
	 * The serial version UID.
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Default create method
	 * @throws CreateException
	 * @ejb.create-method 
	 */
	public void ejbCreate() throws CreateException {
	}
	
	/**
	 * Removes a <code>AccountLite</code> object from the database.
	 * 
	 * @param accountId a <code>Long</code> object being the identifier of the account.
	 * @throws ObjectNotFoundException if the member was not found.
	 * 
	 * @ejb:interface-method view-type="local"
	 */
	public void removeAccount(Long accountId) throws ObjectNotFoundException {
		try {
			AccountLocal account = EntityHomeCache.getInstance().getAccountHome()
					.findByPrimaryKey(accountId);
			account.remove();
		} catch (FinderException e) {
			if (e.getClass() == ObjectNotFoundException.class)
				throw (ObjectNotFoundException) e;
			throw new EJBException(e);
		} catch (RemoveException e) {
			throw new EJBException(e);
		}
	}
	
	/**
	 * Removes a <code>EntryLite</code> object from the database.
	 * 
	 * @param entryId a <code>Long</code> object being the identifier of the entry.
	 * @throws ObjectNotFoundException if the entry was not found.
	 * 
	 * @ejb:interface-method view-type="local"
	 */
	public void removeEntry(Long entryId) throws ObjectNotFoundException {
		try {
			EntryLocal entry = EntityHomeCache.getInstance().getEntryHome()
					.findByPrimaryKey(entryId);
			entry.remove();
		} catch (FinderException e) {
			if (e.getClass() == ObjectNotFoundException.class)
				throw (ObjectNotFoundException) e;
			throw new EJBException(e);
		} catch (RemoveException e) {
			throw new EJBException(e);
		}
	}
	
	/**
	 * Removes a <code>StructureLite</code> object from the database.
	 * 
	 * @param structureId a <code>Long</code> object being the identifier of the structure.
	 * @throws ObjectNotFoundException if the structure was not found.
	 * 
	 * @ejb:interface-method view-type="local"
	 */
	public void removeStructure(Long structureId) throws ObjectNotFoundException {
		try {
			StructureLocal structure = EntityHomeCache.getInstance().getStructureHome()
					.findByPrimaryKey(structureId);
			structure.remove();
		} catch (FinderException e) {
			if (e.getClass() == ObjectNotFoundException.class)
				throw (ObjectNotFoundException) e;
			throw new EJBException(e);
		} catch (RemoveException e) {
			throw new EJBException(e);
		}
	}
	
	/**
	 * Removes a <code>VersionLite</code> object from the database.
	 * 
	 * @param versionId a <code>Long</code> object being the identifier of the version.
	 * @throws ObjectNotFoundException if the version was not found.
	 * 
	 * @ejb:interface-method view-type="local"
	 */
	public void removeVersion(Long versionId) throws ObjectNotFoundException {
		try {
			VersionLocal version = EntityHomeCache.getInstance().getVersionHome()
					.findByPrimaryKey(versionId);
			version.remove();
		} catch (FinderException e) {
			if (e.getClass() == ObjectNotFoundException.class)
				throw (ObjectNotFoundException) e;
			throw new EJBException(e);
		} catch (RemoveException e) {
			throw new EJBException(e);
		}
	}
}
